package filesearch

import javax.swing.JSplitPane
import java.awt.BorderLayout
import net.miginfocom.swing.MigLayout

import org.viewaframework.widget.swing.table.*

import java.awt.Color
import java.awt.Font
import org.jfree.chart.labels.PieToolTipGenerator
import org.jfree.chart.ChartFactory
import org.jfree.chart.ChartPanel
import org.jfree.data.general.DefaultPieDataset

import filesearch.render.*

import groovy.swing.SwingBuilder
import java.awt.*
import javax.swing.WindowConstants as WC

/* Setting some variables */
   piedataset = new DefaultPieDataset()
   fileChooserWindow = fileChooser(fileSelectionMode:JFileChooser.DIRECTORIES_ONLY)
/* Setting default chart values */
   piedataset.with {
	 setValue "No Match", model.noFiles
	 setValue "Match", model.noFilesMatch
   }
   options = [true, true, true]
   chart = ChartFactory.createPieChart("",piedataset, *options)
   chart.backgroundPaint = Color.white
   
/* Setting the application frame */
   application(title: 'fileSearch',
	   size: Toolkit.getDefaultToolkit().getScreenSize(),
	   pack: false,
	   location: [0,0],
	   layout:new java.awt.BorderLayout(),
	   locationByPlatform:true,
	   iconImage: imageIcon('/griffon-icon-48x48.png').image,
	   iconImages: [
		   imageIcon('/griffon-icon-48x48.png').image,
		   imageIcon('/griffon-icon-32x32.png').image,
		   imageIcon('/griffon-icon-16x16.png').image
	   ]) {
		/* -------------------------------------------------- */
		/* -------------------- MENU BAR -------------------- */
		/* -------------------------------------------------- */
		   menuBar{
			   menu('Application'){
					menuItem(text:'Exit',icon:imageIcon('/griffon-icon-16x16.png'),
						actionPerformed:controller.exitApplication)   
			   }			   
		   }
		   /* -------------------------------------------------- */
		   /* -------------------- LAYOUT ---------------------- */
		   /* -------------------------------------------------- */
		   splitPane {
			   splitPane(id:'searchManagerPanel',orientation:JSplitPane.VERTICAL_SPLIT){	   
				   dockingFrame(preferredSize: new java.awt.Dimension(300,300)) {
					   panel(title:'Search Criteria'){
						   borderLayout()
						   panel(name:'mainPanel',layout: new MigLayout("fill"),constraints:java.awt.BorderLayout.NORTH){
							   label(text:'Where to find',name:'whereLabel',constraints:'wrap')
							   textField(name:'whereField',constraints:'grow',enabled:false,text: bind('filePath', target:model, id:'whereField'))
							   button(
								   text:'Directory',
								   name:'whereButton',
								   preferredSize:new java.awt.Dimension(40,20),
								   constraints:'wrap',
								   actionPerformed:controller.browseFiles)						   
							   label(text:'File',name:'fileLabel',constraints:'grow, wrap')
							   textField(id:'fileField',name:'fileField',constraints:'span, grow, wrap',
								   text: bind('fileRegex',target:model,id:'fileField'),
								   enabled: bind(source:model,sourceProperty:'searchEnabled'),
								   actionPerformed:controller.searchFiles)
							   button(
								   name:'searchButton',
								   constraints:'span, grow',
								   text:'search',
								   actionPerformed:controller.searchFiles,
								   enabled: bind{model.dirty})
							/* Enabling search button only when user inserts any file regex */
							   bean( model, dirty:bind {fileField.text != ""})
						   }
					   }
				   }
				   dockingFrame{
					   panel(id:'chartPanel',name:'chartPanel',title:'Search Statistics'){
						 widget(new ChartPanel(chart))
					   }
				   }
			   }
			   dockingFrame{
				   panel(title:'Search Results'){
					   borderLayout()
					   busyComponent(id: "c1", constraints: CENTER, busy: bind(source:model,sourceProperty:'searchResultsPanelEnabled')) {
						   busyModel(description: "Please Wait...")						   
						   scrollPane(name:'peopleTableWrapper',constraints:BorderLayout.CENTER) {
							   widget(id:"dynamicTable",new ViewaWidgetBuilder().dynamicTable {
								   dynamicColumns{
									   dynamicColumn(order:0,propertyName:"name",width:400,title:"Filename",renderer:new FileNameRendererByType())
									   dynamicColumn(order:1,propertyName:"type",width:100,title:"Type")
									   dynamicColumn(order:2,propertyName:"size",width:100,title:"File Length")
									   dynamicColumn(order:3,propertyName:"lastModified",width:100,title:"Last Modified",renderer: new LastModifiedRenderer())
									   dynamicColumn(order:4,propertyName:"path",width:600,title:"File Path")
								   }
							   })
						   }
					   }
				   }				   			
		   	   }	   			   			   
		   }
}
